home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3310 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  96 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c,comp.unix.programmer
  4. Subject: Re: Interprocess Communication in Linux
  5. Date: Sat, 27 Jan 96 18:03:48 GMT
  6. Organization: none
  7. Distribution: world
  8. Message-ID: <822765828snz@genesis.demon.co.uk>
  9. References: <4e768q$sfc@news1.infinet.com>
  10. Reply-To: fred@genesis.demon.co.uk
  11. X-NNTP-Posting-Host: genesis.demon.co.uk
  12. X-Newsreader: Demon Internet Simple News v1.27
  13. X-Mail2News-Path: genesis.demon.co.uk
  14.  
  15. In article <4e768q$sfc@news1.infinet.com>
  16.            jmonnin@infinet.com "Joe Monnin" writes:
  17.  
  18. >I'm having a problem sending a message from one process to another in Linux. 
  19. > My function reads something like:
  20. >
  21. >
  22. >void function{
  23.  
  24. I assume you meant:
  25.  
  26. void function(){
  27.  
  28. >        struct msgbuf message;
  29. >
  30. >        msgid=1;
  31.  
  32. I must assume 1 is somehow a valid message queue ID
  33.  
  34. >        result=msgsnd(msgid,&message,MSGMAX,IPC_NOWAIT);
  35. >}
  36.  
  37. While msgsnd() isn't a part of the C language it is worth looking at the
  38. language issues in this question. Other discussion should be directed to
  39. comp.unix.programmer or a Linux newsgroup. Since this is of Unix
  40. interest I've cross-posed to comp.unix.programmer.
  41.  
  42. The msgsnd prototype is:
  43.  
  44. int msgsnd(int msqid, struct msgbuf *msgp, int msgsz, int msgflg);
  45.  
  46. The problem is that struct msgbuf has a sort of 'struct hack' definition
  47. which is deemed illegal in ANSI C and results in the whole thing being
  48. rather messy. If you look in your header files you'll find
  49. struct msgbuf declared as something like:
  50.  
  51. struct msgbuf {
  52.     long    mtype;
  53.     char    mtext[1];
  54. };
  55.  
  56. As defined your variable message has room for just 1 'data' byte. So if
  57. MSGMAX is more than 1 you're telling msgsnd() to use memory outside the
  58. bounds of your allocated object which is illegal...
  59.  
  60. >the functions returns a -1 indiacting an error, and errno is set to EFAULT
  61. > which my documention says means "address
  62. >pointed to by message is not accessable."  Why isn't it accessable?  It would
  63. > be to any other function.
  64.  
  65. and could easily result in this error. What you need to do in this case is
  66. define your own structure type as, say:
  67.  
  68. struct my_msgbuf {
  69.     long    mtype;
  70.     char    mtext[MSGMAX];
  71. }
  72.  
  73. and use:
  74.  
  75. void function(void)
  76. {
  77.         int msgid, result;
  78.         struct my_msgbuf message;
  79.  
  80.         /* Fill message */
  81.  
  82.         msgid=1;
  83.         result=msgsnd(msgid,(struct msgbuf *)&message,MSGMAX,IPC_NOWAIT);
  84. }
  85.  
  86. This is an example of a very poorly designed system interface. It could
  87. have been trivially fixed by making mtype an argument to msgsnd thus
  88. making the 2nd argument a simple char * or void *, which then allows
  89. you to pass any type of object through msgsnd/msgrcv.
  90.  
  91. -- 
  92. -----------------------------------------
  93. Lawrence Kirby | fred@genesis.demon.co.uk
  94. Wilts, England | 70734.126@compuserve.com
  95. -----------------------------------------
  96.